package com.thingcom.delinrobot.main.Launch.NewUser;

import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizUserAccountType;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiSDKListener;
import com.google.android.material.textfield.TextInputLayout;
import com.thingcom.delinrobot.Common.Constant;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseBackFragment;
import com.thingcom.delinrobot.utils.MyLog;
import com.thingcom.delinrobot.utils.PreferencesUtil;
import com.thingcom.delinrobot.utils.ToastUtil;
import com.thingcom.delinrobot.view.SimpleToolbar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.thingcom.delinrobot.Common.Constant.MAIL_S;
import static com.thingcom.delinrobot.utils.AppUtils.hideSoftKeyboard;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/11/29
 */
public class RegisterPasswordFragment extends BaseBackFragment {

    @BindView(R.id.toolbar_login_password)
    SimpleToolbar toolbarLoginPassword;
    @BindView(R.id.et_register_password)
    EditText etRegisterPassword;
    @BindView(R.id.til_login_password)
    TextInputLayout tilLoginPassword;
    @BindView(R.id.cl_register_password)
    ConstraintLayout clRegisterPassword;
    @BindView(R.id.btn_register_password_continue)
    Button btnRegisterPasswordContinue;
    @BindView(R.id.checkBox)
    CheckBox checkBox;
    @BindView(R.id.tv_privacy_agreement)
    TextView tvPrivacyAgreement;
    private String mail;

    public static RegisterPasswordFragment newInstance() {
        Bundle args = new Bundle();
        RegisterPasswordFragment fragment = new RegisterPasswordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_back_register_password, container, false);
        unbinder = ButterKnife.bind(this, view);
        initToolbar(toolbarLoginPassword);
        ColorStateList colorStateList = getResources().getColorStateList(R.color.text_input_edit_selectoe);
        tilLoginPassword.setBoxStrokeColorStateList(colorStateList);
        btnRegisterPasswordContinue.setEnabled(false);
        btnRegisterPasswordContinue.setBackgroundColor(getResources().getColor(R.color.grey_button));
        clRegisterPassword.setOnTouchListener((v, event) -> {
            clRegisterPassword.clearFocus();
            hideSoftKeyboard(getContext(), clRegisterPassword);
            return false;
        });
        mail = PreferencesUtil.getInstance(getContext()).getString(Constant.MAIL_NUMBER, null);
        return view;

    }

    @OnClick({R.id.btn_register_password_continue,R.id.checkBox,R.id.tv_privacy_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_register_password_continue:
                hideSoftKeyboard(_mActivity);
                String registerPsw = etRegisterPassword.getText().toString();
                PreferencesUtil.getInstance(getContext()).putString(MAIL_S, registerPsw);
                GizWifiSDK.sharedInstance().setListener(mListener);
                // 调用SDK的邮箱号注册接口
                GizWifiSDK.sharedInstance().registerUser(mail, registerPsw, null, GizUserAccountType.GizUserEmail);
                MyLog.i("注册", "mail: " + mail);

                break;
            case R.id.checkBox:
                if (checkBox.isChecked()) {
                    btnRegisterPasswordContinue.setEnabled(true);
                    btnRegisterPasswordContinue.setBackground(getResources().getDrawable(R.drawable.selector_button_main));
                } else {
                    btnRegisterPasswordContinue.setEnabled(false);
                    btnRegisterPasswordContinue.setBackgroundColor(getResources().getColor(R.color.grey_button));
                }
                break;

            case R.id.tv_privacy_agreement:
                start(PrivacyAgreementFragment.newInstance());
                break;
        }

    }

    // 实现回调
    private GizWifiSDKListener mListener = new GizWifiSDKListener() {
        @Override
        public void didRegisterUser(GizWifiErrorCode result, String uid, String token) {
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                ToastUtil.showToast(getContext(), getString(R.string.success_toast));
                start(RegisterSuccessFragment.newInstance());
                // 注册成功
                PreferencesUtil.getInstance(getContext()).putString(Constant.UID, uid);
                PreferencesUtil.getInstance(getContext()).putString(Constant.TOKEN, token);

            } else if (result == GizWifiErrorCode.GIZ_OPENAPI_EMAIL_UNAVALIABLE) {
                ToastUtil.showToast(getContext(), getString(R.string.giz_email_unavailable));
            } else {
                ToastUtil.showToast(getContext(), getString(R.string.fail_toast));
                MyLog.i("注册", "didRegisterUser: " + result);
                // 注册失败
            }
        }
    };


    }



